#!/usr/bin/env python 2.6
#=============================================================================
'''
    Execute test scripts for Surveyor
'''
#=============================================================================
# Copyright 2011, Matt Peloquin. This file is part of Code Surveyor,
# covered under GNU GPL v3 and is distributed WITHOUT ANY WARRANTY
#=============================================================================
import sys
import unittest

import unit_test
import csmodule_test
import system_test

suiteMapping = {
    "unit": unit_test.suite,
    "module": csmodule_test.suite,
    "system": system_test.suite,
    }

def suite(suiteName=None, testName=None, extraArg1=None, extraArg2=None):
    testList = []
    if suiteName is None:
        for testSuiteFactory in suiteMapping.values():
            testList.append(testSuiteFactory())
    else:
        if testName is None:
            testList.append(suiteMapping[suiteName]())
        else:
            testList.append(suiteMapping[suiteName](testName, extraArg1, extraArg2))
    return unittest.TestSuite(testList)


if __name__ == '__main__':

    testSuite = suite()
    if len(sys.argv) > 1 and len(sys.argv) <= 5:
        testSuite = suite(*tuple(arg for arg in sys.argv[1:]))
    else:
        print "Usage: run_scripts.py [unit|module|system] [testName args1 args2]\n"

    # Spit out Dos return value for integration with Windows bat files
    if unittest.TextTestRunner(verbosity=3).run(testSuite).wasSuccessful():
        sys.exit(0)
    else:
        sys.exit(1)


